#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os.path
import argparse

from file_util import mkdir

def gen_arktsconfig(config_path, base_url_path, ets_plugins_path, arktsconfig_template, arktsconfig_file):
    mkdir(config_path)
    if os.path.exists(arktsconfig_file):
        os.remove(arktsconfig_file)
    with open(arktsconfig_template, 'r') as config_file:
        data = json.load(config_file)
        data['compilerOptions']['baseUrl'] = base_url_path
        data['compilerOptions']['paths']['std'] = [os.path.join(ets_plugins_path, 'std')]
        data['compilerOptions']['paths']['escompat'] = [os.path.join(ets_plugins_path, 'escompat')]
        with open(arktsconfig_file, 'w') as out_file:
            out_file.write(json.dumps(data, indent=2))

def main():
    parser = argparse.ArgumentParser(description='generate arktsconfig.json')
    parser.add_argument('--arkcompiler_tool_path', help='arkcompiler tool path')
    parser.add_argument('--arktsconfig_template', help='arktsconfig template file')
    parser.add_argument('--src_root', help='source ets file root path')
    parser.add_argument('--build_root', help='build dir root path')

    args = parser.parse_args()
    ets_plugins_path = os.path.join(args.arkcompiler_tool_path, 'plugins', 'ets', 'stdlib')
    arktsconfig_file = os.path.join(args.build_root, 'arktsconfig.json')

    gen_arktsconfig(
        args.build_root,
        args.src_root,
        ets_plugins_path,
        args.arktsconfig_template,
        arktsconfig_file
    )

if __name__ == '__main__':
    main()